/**
 * 系统管理 用户管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <!-- <el-select size="small" v-model="formInline.isLock" placeholder="请选择">
          <el-option label="全部" value=""></el-option>
          <el-option label="正常" value="N"></el-option>
          <el-option label="已锁定" value="Y"></el-option>
        </el-select> -->
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.user_name" placeholder="输入用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="userData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <!-- <el-table-column align="center" type="selection" width="50">
      </el-table-column> -->
      <el-table-column align="center" type="index" width="60">
      </el-table-column>
      <el-table-column align="center" sortable prop="user_code" label="用户ID" width="300">
      </el-table-column>
      <el-table-column align="center" sortable prop="user_name" label="用户名" width="300">
      </el-table-column>
      <el-table-column align="center" sortable prop="tel_no" label="电话号" width="300">
      </el-table-column>
      <!-- <el-table-column align="center" sortable prop="role_code" label="类型" min-width="80">
      </el-table-column> -->
      <el-table-column align="center" sortable label="类型" width="300" show-overflow-tooltip>
        <template slot-scope="scope">
          <div>
            {{formatrole(scope.row)}}
          </div>
        </template>  
      </el-table-column> 
      <el-table-column label="操作" min-width="80" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="setRolecode('rider', scope.row)">设为骑手</el-button>
          <el-button size="mini" type="success" @click="setRolecode('operator', scope.row)">设操作员</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
  </div>
</template>

<script>
// 导入请求方法
import {
  userList,
  userSave,
  userDelete,
  userPwd,
  userExpireToken,
  userFlashCache,
  userLock,
  UserDeptTree,
  UserDeptSave,
  UserDeptdeptTree,
  UserChangeDept,
  settingRider
} from '../../api/userMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      loading: false, //是显示加载
      title: '添加用户',
      // 编辑与添加
      editForm: {
        userId: '',
        userName: '',
        userRealName: '',
        roleId: '',
        userMobile: '',
        userEmail: '',
        userSex: '',
        token: localStorage.getItem('logintoken')
      },
      // 设置为骑手类型
      settingRole:{
        "operateusercode":localStorage.getItem('usercode'),//操作人员工号
        "usercode":"", //用户id
        "userrole" :0  //0用户，1骑手
      },
      // 选择数据
      selectdata: [],   
      // 请求数据参数--用户列表参数
      formInline: {
        page: 1,
        limit: 10,
        deptId: '',
        user_name: '',  ///按用户名搜索
        tel_no: '',
        isLock: '',
        rolecode: 0  //0为普通用户，1为回收员
      },
      //用户数据
      userData: [],
      // 数据权限
      UserDept: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 选中
      checkmenu: [],
      //参数role
      saveroleId: '',
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    getdata(parameter) {
      this.loading = true
      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      // 获取用户列表
      userList(parameter).then(res => {
        this.loading = false
        if (res.data.data.userlist.length == 0) {
          this.$message({
            type: 'info',
            message: '没有查询到用户'
          })
        } else {
          // console.log('wiatAssign-user:',res)
          this.userData = res.data.data.userlist
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page
          this.pageparm.pageSize = this.formInline.limit
          // 总数
          this.pageparm.total = res.data.data.length
        }
      })
    },
    // 转换状态
    formatrole(item){
      let rolecode = `${item.role_code}`
      return rolecode==0 ? '普通用户':'回收员'
    },
    // 将普通用户设置为骑手和操作员
    setRolecode(option,row){
      this.settingRole.usercode = row.user_code
      if (option == 'rider'){
        this.settingRole.userrole = 1
      }
      else if (option == 'operator'){
        this.settingRole.userrole = 2
      }      
      // console.log('settingRider:',this.settingRole)
      // 修改状态
      settingRider(this.settingRole)
      .then(res => {
        console.log('settingRider:',res)
        this.loading = false
        if (res.msg != '更新成功') {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.$message({
            type: 'success',
            message: '设置成功'
          })
          this.getdata(this.formInline.rolecode)
        }
      })

    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    //搜索事件
    search() {
      this.formInline.page = 1
      this.getdata(this.formInline)
    }, 
    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val
    },
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 